home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / vsrc.tar / voyager7_src / bintomem.c next >
C/C++ Source or Header  |  1991-02-27  |  3KB  |  149 lines

  1. /*
  2. // Abstract:
  3. //    BINTOMEM
  4. //
  5. //    Converts a binary file (BIN) to Voyager format (MEM).
  6. //
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define DEBUG 0
  14. #define OVERHEAD 8
  15. #define MAX_IBUF 256
  16. #define MAX_OBUF 513
  17.  
  18. #define vgr__openin \
  19. "%%VOYAGER-E-OPENIN, error opening %s as input\n"
  20.  
  21. #define vgr__openout \
  22. "%%VOYAGER-E-OPENOUT, error opening %s as output\n"
  23.  
  24. #define vgr__usage \
  25. "%%VOYAGER-I-USAGE, usage is: BINTOMEM input[.] [output[.MEM]]\n"
  26.  
  27. format_data(char *ibuf, size_t length, char *obuf);
  28.  
  29. FILE *ifile;
  30. FILE *ofile;
  31. char drive[_MAX_DRIVE];
  32. char subdirectory[_MAX_DIR];
  33. char filename[_MAX_FNAME];
  34. char extension[_MAX_EXT];
  35. char ipath[_MAX_PATH];
  36. char opath[_MAX_PATH];
  37. char ibuf[MAX_IBUF];
  38. char obuf[MAX_OBUF];
  39. size_t length;
  40.  
  41. main(int argc, char *argv[])
  42. {
  43.     /*
  44.     // Check for correct number of parameters.
  45.     */
  46.  
  47.     if (argc < 2 || argc > 3) {
  48.         printf(vgr__usage);
  49.         exit(-1);
  50.     }
  51.  
  52.     /*
  53.     // Get the path for the input file and supply defaults.
  54.     */
  55.  
  56.     _splitpath(argv[1],drive,subdirectory,filename,extension);
  57.     if (strcmp(extension,"") == 0) strcpy(extension,".");
  58.     _makepath(ipath,drive,subdirectory,filename,extension);
  59.  
  60.     /*
  61.     // Get the path for the output file and supply defaults.
  62.     */
  63.  
  64.     if (argc == 3) {
  65.         _splitpath(argv[2],drive,subdirectory,filename,extension);
  66.         if (strcmp(extension,"") == 0) strcpy(extension,".MEM");
  67.         _makepath(opath,drive,subdirectory,filename,extension);
  68.     } else {
  69.         strcpy(extension,".MEM");
  70.         _makepath(opath,NULL,NULL,filename,extension);
  71.     }
  72.  
  73.     /*
  74.     // Convert to uppercase.
  75.     */
  76.  
  77.     strupr(ipath);
  78.     strupr(opath);
  79.  
  80. #if DEBUG
  81.     printf("Input file:   %s\n",ipath);
  82.     printf("Output file:  %s\n",opath);
  83. #endif
  84.     /*
  85.     // Open the input file.
  86.     */
  87.  
  88.     ifile = fopen(ipath, "rb");
  89.  
  90.     if (ifile == NULL) {
  91.         fprintf(stderr, vgr__openin, ipath);
  92.         exit(-1);
  93.     }
  94.  
  95.     /*
  96.     // Open the output file.
  97.     */
  98.  
  99.     ofile = fopen(opath, "wt");
  100.  
  101.     if (ofile == NULL) {
  102.         fprintf(stderr, vgr__openin, opath);
  103.         exit(-1);
  104.     }
  105.  
  106.     /*
  107.     // Skip over the header.
  108.     */
  109.  
  110.     length = fread(ibuf,sizeof(char),OVERHEAD,ifile);
  111.     ibuf[length] = '\0';
  112.     printf("Header: %s\n", ibuf);
  113.  
  114.     /*
  115.     // Read from the input and write to the output, reformatting as
  116.     // as we go.
  117.     */
  118.  
  119.     while (!feof(ifile)) {
  120.         length = fread(ibuf,sizeof(char),MAX_IBUF,ifile);
  121.         if (length != 0) {
  122.             format_data(ibuf,length,obuf);
  123.             fputs(obuf, ofile);
  124.         }
  125.     }
  126.  
  127.     /*
  128.     // Close the files.
  129.     */
  130.  
  131.     fclose(ifile);
  132.     fclose(ofile);
  133. }
  134.  
  135. format_data(char *ibuf, size_t length, char *obuf)
  136. {
  137.     int i;
  138.     char tmp[3];
  139.  
  140.     for (i = 0; i < length; i++) {
  141.         sprintf(tmp,"%02X", (unsigned char) *ibuf);
  142.         *obuf++ = tmp[1];
  143.         *obuf++ = tmp[0];
  144.         ibuf++;
  145.     }
  146.  
  147.     *obuf = '\0';
  148. }
  149.